package com.ctg.payroll2.service.cusadmin.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.CsProduct;
import com.ctg.payroll2.bean.po.PbCompany;
import com.ctg.payroll2.bean.po.PbCompanyLinkman;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.dao.cusadmin.PbAccountDAO;
import com.ctg.payroll2.dao.cusadmin.PbCompanyDAO;
import com.ctg.payroll2.dao.cusadmin.PbCompanyLinkManDAO;
import com.ctg.payroll2.dao.publics.PbProductDAO;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@SuppressWarnings("unchecked")
public class PbCompanyServiceImpl implements
		com.ctg.payroll2.service.cusadmin.PbCompanyService {
	private PbCompanyDAO pbCompanyDAO;
	private PbCompanyLinkManDAO companyLinkManDAO;
	private PbProductDAO productDAO;
	private PbAccountDAO pbAccountDao;

	public PbProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(PbProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	public PbCompanyDAO getPbCompanyDAO() {
		return pbCompanyDAO;
	}

	public void setPbCompanyDAO(PbCompanyDAO pbCompanyDAO) {
		this.pbCompanyDAO = pbCompanyDAO;
	}

	public PbAccountDAO getPbAccountDao() {
		return pbAccountDao;
	}

	public void setPbAccountDao(PbAccountDAO pbAccountDao) {
		this.pbAccountDao = pbAccountDao;
	}

	public Object getBeanfromJson(String jstr, Class clazz, String objname) {
		Object obj = null;
		JSONObject js = JSONObject.fromObject(jstr);
		if (js != null) {
			obj = JSONObject.toBean(js.getJSONObject(objname), clazz);
		}
		return obj;
	}

	public List getBeanListfromjson(String jstr, Class clazz, String objname) {
		List<PbCompanyLinkman> list = new ArrayList<PbCompanyLinkman>();
		JSONObject js = JSONObject.fromObject(jstr);
		JSONArray ja = js.getJSONArray(objname);
		for (Iterator<JSONObject> it = ja.iterator(); it.hasNext();) {
			PbCompanyLinkman lm = (PbCompanyLinkman) JSONObject.toBean(it
					.next(), PbCompanyLinkman.class);
//			System.out.println(lm.getLink_name());
			list.add(lm);
		}
		return list;
	}

	public List getcsproBeanListfromjson(String jstr, Class clazz,
			String objname) {
		List<CsProduct> list = new ArrayList<CsProduct>();
		JSONObject js = JSONObject.fromObject(jstr);
		JSONArray ja = js.getJSONArray(objname);
		for (Iterator<JSONObject> it = ja.iterator(); it.hasNext();) {
			CsProduct lm = (CsProduct) JSONObject.toBean(it.next(), clazz);
			list.add(lm);
		}
		return list;
	}

	public Boolean insert(String jstr) {
		Boolean returnval = false;
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser sysuer = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);

		if (null != sysuer) {
			PbCompany com = (PbCompany) getBeanfromJson(jstr, PbCompany.class,
					"company");
			List<PbCompanyLinkman> list = getBeanListfromjson(jstr,
					PbCompanyLinkman.class, "linkmans");
			List<CsProduct> csplist = getcsproBeanListfromjson(jstr,
					CsProduct.class, "procudts");
			try {
				if (!isSig(com.getCompany_code())) {
					pbCompanyDAO.insert(com);
					for (PbCompanyLinkman linkman : list) {
						linkman.setCompany_code(com.getCompany_code());
						companyLinkManDAO.insert(linkman);
					}
					if (csplist.size() > 0) {
						pbCompanyDAO.deleteCsKey(com.getCompany_code());
						for (CsProduct csProduct : csplist) {
							csProduct.setCompany_id(com.getCompany_code());
							insertCsProduct(csProduct);
						}
						HashMap<String, Object> insertMap = new HashMap<String, Object>();
						insertMap.put("company_code", com.getCompany_code());
						insertMap.put("creator_id", sysuer.getEmployeeId());
						insertMap.put("create_code", sysuer.getLoginId());
						pbCompanyDAO.insertCsKey(insertMap);
					}
					returnval = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			throw new RuntimeException("session过期或者不存在");
		}
		return returnval;
	}

	/**
	 * 公司注册页面的现实与分页查询
	 */
	public Page getAllPbCompanyByPaging(String company_code,
			String company_chinese_code, int status, String start_date,
			String end_date, int start, int limit) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (company_code != null) {
			parameters.put("company_code", company_code);
		}
		if (company_chinese_code != null) {
			parameters.put("company_chinese_code", company_chinese_code);
		}
		if (status != 1) {
			parameters.put("status", status);
		}
		if (start_date != null) {
			parameters.put("start_date", start_date);
		}
		if (end_date != null) {

			parameters.put("end_date", end_date);
		}
		return pbCompanyDAO.pagedQuery(parameters, start, limit);
	}

	public PbCompanyLinkManDAO getCompanyLinkManDAO() {
		return companyLinkManDAO;
	}

	public void setCompanyLinkManDAO(PbCompanyLinkManDAO companyLinkManDAO) {
		this.companyLinkManDAO = companyLinkManDAO;
	}

	/*
	 * 修改OR新增linkman
	 * 
	 * @see com.ctg.payroll2.service.cusadmin.PbCompanyService#update(java.lang.String)
	 */
	public void update(String jstr) {
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser sysuer = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);
		if (null != sysuer) {
			HashMap<String, Object> delCspro = new HashMap<String, Object>();
			PbCompany com = (PbCompany) getBeanfromJson(jstr, PbCompany.class,
					"company");
			List<PbCompanyLinkman> linkmanlist = getBeanListfromjson(jstr,
					PbCompanyLinkman.class, "linkmans");
			List<CsProduct> csproductlist = getcsproBeanListfromjson(jstr,
					CsProduct.class, "procudts");
			try {
				pbCompanyDAO.update(com);
				for (PbCompanyLinkman linkman : linkmanlist) {
					if (linkman.getLink_id() != null) {
						companyLinkManDAO.update(linkman);
					} else {
						linkman.setCompany_code(com.getCompany_code());
						companyLinkManDAO.insert(linkman);
					}
				}
				delCspro.put("company_code", com.getCompany_code());
				if (csproductlist.size() > 0) {
					pbCompanyDAO.deleteCsproductBycompanyCode(delCspro);

					pbCompanyDAO.deleteCsKey(com.getCompany_code());
					for (CsProduct csProduct : csproductlist) {
						csProduct.setCompany_id(com.getCompany_code());
						insertCsProduct(csProduct);
					}
					HashMap<String, Object> insertMap = new HashMap<String, Object>();
					insertMap.put("company_code", com.getCompany_code());
					insertMap.put("creator_id", sysuer.getEmployeeId());
					insertMap.put("create_code", sysuer.getLoginId());
					pbCompanyDAO.insertCsKey(insertMap);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			throw new RuntimeException("session 失效或者已过期");
		}
	}

	/**
	 * 客户从停用到启用的状态更新
	 * 
	 * @return
	 */
	public void updateStatus(String company_code) {
		pbCompanyDAO.update(".PbCompanyupdateStatus", company_code);
	}

	/*
	 * @see com.ctg.payroll2.service.cusadmin.PbCompanyService#getLinkMan(查询条件,页开始数,页结束数r)
	 */
	public Page getLinkMan(Object paraMap, Integer start, Integer limit) {
		Page page = companyLinkManDAO.pagedQuery((Map<String, Object>) paraMap,
				start, limit);
		// != null ? companyLinkManDAO.pagedQuery((Map<String, Object>) paraMap,
		// start, limit) : new Page();
		return page;

	}

	public void deleteLinkMan(String jstr) {
		List<PbCompanyLinkman> list = getBeanListfromjson(jstr,
				PbCompanyLinkman.class, "deletelinkmans");
		try {
			for (PbCompanyLinkman linkman : list) {
				if (linkman.getLink_id() != null) {
					companyLinkManDAO.deleteByPrimaryKey(linkman.getLink_id());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Page getProduct(Object paraMap, Integer start, Integer limit) {
		Page propage = null;
		try {
			propage = productDAO.pagedQuery((Map) paraMap, 0, 100);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return propage;
	}

	public void insertCsProduct(Object obj) {
		if (obj.getClass().isArray()) {
			List<CsProduct> csProducts = (List<CsProduct>) obj;
			for (CsProduct csProduct : csProducts) {
				pbCompanyDAO.insertCsPrpduct(csProduct);
			}
		} else {
			pbCompanyDAO.insertCsPrpduct((CsProduct) obj);
		}
	}

	public void insertstop_reason(HashMap<String, Object> paraMap) {
		pbCompanyDAO.insertstop_reason(paraMap);
	}

	public List<HashMap<String, Object>> select_hisstop(
			HashMap<String, Object> paraMap) {
		return pbCompanyDAO.select_hisstop(paraMap);
	}

	public Boolean isSig(String company_code) {
		return pbCompanyDAO.isSig(company_code);
	}

	public Boolean deleteCsKey(String company_code) {
		return pbCompanyDAO.deleteCsKey(company_code);
	}

	public void insertCsKey(HashMap<String, Object> insertMap) {
		pbCompanyDAO.insertCsKey(insertMap);
	}
}
